<template>
	 <div class="add-node-btn-box">
	 	<div class="add-node-btn">
			<el-popover placement="right-start" :width="351" trigger="click" :hide-after="0" :show-after="0" ref="popover">
				<template #reference>
					<el-button type="primary" icon="el-icon-plus" circle></el-button>
				</template>
				<div class="add-node-popover-body">
					<ul class="node-select">
						<li  @click="addType(1)">
							<el-icon style="color: #ff943e;"><el-icon-user-filled /></el-icon>
							<p class="node-title">审批节点</p>
						</li>
						<li @click="addType(2)">
							<el-icon style="color: #3296fa;" ><el-icon-promotion /></el-icon>
							<p class="node-title">抄送节点</p>
						</li>
						<li @click="addType(4)">
							<el-icon style="color: #15BC83;"><el-icon-share /></el-icon>
							<p class="node-title">条件分支</p>
						</li>
						<li @click="addType(5)">
							<el-icon style="color: rgb(113, 141, 255);"><el-icon-expand /></el-icon>
							<p class="node-title">并行分支</p>
						</li>
						<li @click="addType(6)">
							<el-icon style="color: rgb(242, 86, 67);"><el-icon-alarm-clock /></el-icon>
							<p class="node-title">延迟等待</p>
						</li>
						<li @click="addType(7)">
							<el-icon style="color: rgb(21, 188, 131);"><el-icon-set-up /></el-icon>
							<p class="node-title">事件触发</p>
						</li>
					</ul>
				</div>
			</el-popover>
	 	</div>
	 </div>
</template>

<script>
import SnowflakeId from "snowflake-id";
	export default {
		props: {
			// 父节点数据
			parentData:{},
			modelValue: { type: Object, default: () => {} }
		},
		data() {
			return {
				snowflake:null,
				flowData:null,
			}
		},
		mounted() {
			const snowflake = new SnowflakeId({
				mid: 42,
				offset: (2019 - 1970) * 31536000 * 1000
			});
			this.snowflake =snowflake;
		},
		methods: {
			check(){
				if (typeof(this.parentData.childNode) != 'undefined'){
					this.$message.error("请先删除该节点下所有子节点后重新添加")
					return false;
				}else {
					return true;
				}
			},
			// 校验是否能添加审批节点  1:审批人节点  2:抄送节点  4:分支节点 5:并行节点  6:延迟节点  7:事件节点  8：同意/拒绝
			addType(type){
				var nodeId = "node_"+this.snowflake.generate();
				if (this.modelValue!=null){
					this.modelValue.parentId = nodeId;
				}
				if (this.parentData.type == 0){
					this.parentData.id = 'root'
				}
				var node = {}
				if (type == 1) {
					node = {
						id: nodeId,  // 节点ID
						parentId: this.parentData.id,
						nodeName: '审批人',  //"审核人",
						type: 1,			//节点类型
						setType: 1,			//审核人类型
						nodeUserList: [],	//审核人成员
						nodeRoleList: [],	//审核角色
						examineLevel: 1,	//指定主管层级
						directorLevel: 1,	//自定义连续主管审批层级
						selectMode: 1,		//发起人自选类型
						timeList:{
							termAuto: false,	//审批期限超时自动审批
							term: 0,			//审批期限
							termMode: 1,		//审批期限超时后执行类型  0 通过  1拒绝 2 发送提醒
						},
						examineMode: 3,		//多人审批时审批方式
						directorMode: 0,	//连续主管审批方式
						rejectNode:"root",  //驳回节点
						taskCompletion:"50",//流程进度
						successBtn:true,    //同意按钮
						successBtnText:'通过',//同意按钮文字
						refuseBtn:true,    //拒绝按钮
						refuseBtnText:'驳回',  //拒绝按钮文字
						referralBtn:true,     //转审按钮
						referralBtnText:'转审',//转审按钮文字
						stopBtn:true,        //终止按钮
						stopBtnText:'结束',   //终止按钮文字
						printBtn:true,       //打印按钮
						printBtnText:'打印',  //打印按钮文字
						sign:false,          //手写签名
						formType:4,          //填报表单 1:同步开始表单 2:选择同步的节点表单 3:选择表单  4无
						nodeForm:null,       //填报表单,
						formOption:[],       //表单权限配置
						dataFormType:1,      //数据表单   1:同步开始表单 2:选择同步的节点表单 3:选择表单
						dataForm:null,       //数据表单,
						formId:null,         // 表单ID
						formNodeId:null,       //表单选择节点ID
						successEvent:false,   // 同意事件是否开启
						successEventId:null,  // 同意事件接口ID
						successEventParam:null,  // 同意事件接口参数
						refuseEvent:false,     // 拒绝事件是否开启
						refuseEventId:null,     // 拒绝事件是否开启
						refuseEventParam:null,     // 拒绝事件是否开启
						successMsg:'sync',		// 节点同意
						successTemplateId:null,  // 成功消息模板ID
						successTemplateParam:null, // 成功消息模板参数
						refuseMsg:'sync',         // 节点拒绝
						refuseMsgTemplateId:null,  // 成功消息模板ID
						refuseMsgTemplateParam:null, // 成功消息模板参数
						waitMsg:'sync',				// 待办消息
						waitMsgTemplateId:null,     // 待办消息模板ID
						waitMsgTemplateParam:null,  // 待办消息模板参数
						endMsg:'sync',				// 结束消息
						endTemplateId:null,			// 结束消息模板ID
						endMsgTemplateParam:null,   // 结束消息模板参数
						timeOutTemplateId:null,  // 超时消息模板ID
						timeOutTemplateParam:null, // 超时消息模板参数
						childNode: this.modelValue,
					}
				}else if(type == 2){
					node = {
						//id: "node_"+this.snowflake.generate(),
						id: nodeId,
						parentId: this.parentData.id,
						nodeName: "抄送人",
						setType: 0,			//抄送人员类型
						examineLevel: 1,	//指定主管层级
						type: 2,
						userSelectFlag: false,
						nodeUserList: [],
						nodeRoleList:[],
						childNode: this.modelValue
					}

				}else if(type == 4){
					node = {
						//id: "node_"+this.snowflake.generate(),
						id: nodeId,
						parentId: this.parentData.id,
						nodeName: "条件路由",
						type: 4,
						conditionNodes: [
							{
								id: "node_"+this.snowflake.generate(),
								nodeName: "条件1",
								type: 3,
								priorityLevel: 1,
								conditionMode: 1,
								conditionList: [],
								parentApprover:null,
							},
							{
								id: "node_"+this.snowflake.generate(),
								nodeName: "条件2",
								type: 3,
								priorityLevel: 2,
								conditionMode: 1,
								conditionList: [],
								parentApprover:null,
							}
						],
						childNode: this.modelValue
					}

				}else if(type == 5){
					if (this.parentData.type == 5){
						this.$message.error("并行网关下不能重复添加")
						return;
					}
					//let id= "node_"+this.snowflake.generate()
					node = {
						// 节点ID
						//id: id,
						id: nodeId,
						// 父级ID
						parentId: this.parentData.id,
						// 节点名称
						nodeName: "并行分支",
						type: 5,
						branchs: [
							{
								// 节点ID
								id: "node_"+this.snowflake.generate(),
								// 父级ID
								//parentId: id,
								parentId: nodeId,
								nodeName: "分支1",
								type: 3,
								priorityLevel: 1,
								conditionMode: 1,
								conditionList: []
							},
							{
								// 节点ID
								id: "node_"+this.snowflake.generate(),
								// 父级ID
								//parentId: id,
								parentId: nodeId,
								nodeName: "分支2",
								type: 3,
								priorityLevel: 2,
								conditionMode: 1,
								conditionList: []
							}
						],
						childNode: this.modelValue
					}

				}else if (type==6){
					node = {
						//id: "node_"+this.snowflake.generate(),
						id: nodeId,
						parentId: this.parentData.id,
						nodeName: "延时事件",
						type: 6,
						userSelectFlag: true,
						nodeUserList: [],
						props: {
							type: "FIXED",
							time: 1,
							unit: "M",
							dateTime: "",
							// 触发事件
							event:"pass",
							taskName:'自动通过'
						},
						childNode: this.modelValue
					}
				}else if (type == 7){
					node = {
						//id: "node_"+this.snowflake.generate(),
						id: nodeId,
						parentId: this.parentData.id,
						nodeName: "触发事件",
						type: 7,
						successEventParam:null,  // 事件参数
						props: {
							taskType: 1,
							// 触发事件
							event:'',
							// 手机号
							phone:'',
							taskName:'调用接口'
						},
						childNode: this.modelValue
					}


				}
				this.$refs.popover.hide()
				this.$emit("update:modelValue", node)

			}
		}
	}
</script>

<style>
.node-select {display: flex;flex-wrap: wrap;}
.node-select li {display: inline-block;margin: 5px 5px;cursor: pointer;padding: 10px 15px;border: 0px solid #f8f9f9;background-color: #f8f9f9;border-radius: 0px;width: 152px;position: relative;}
.node-select li:hover {background-color: #fff;box-shadow: 0 0 5px 2px #d6d6d6;}
.node-select li:hover i {border: 1px solid #3296fa;background: #3296fa;color: #fff!important;}
</style>
